<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>当前的数量是<span>0</span></div>
  <button class="add">+1</button>
  <button class="minus">-1</button>

  <script src="node_modules/redux/dist/redux.js"></script>
  <script>
    //创建action
    const add = () => ({
      type: 'ADD',
      name: '数量+1'
    })
    const minus = () => ({
      type: 'MINUS',
      name: '数量-1'
    })
    //创建reducer
    function reducer(state = 100, action) {
      switch (action.type) {
        case 'ADD':
          return state + 1
        case 'MINUS':
          return state - 1
        default:
          return state
      }
    }
    //创建store
    const { createStore } = window.Redux
    const store = createStore(reducer)
    document.querySelector('span').innerHTML = store.getState()
    store.subscribe(() => {
      console.log(store.getState())
      document.querySelector('span').innerHTML = store.getState()
    })
    document.querySelector('.add').addEventListener('click', () => {
      store.dispatch(add())
    })
    document.querySelector('.minus').addEventListener('click', () => {
      store.dispatch(minus())
    })
  </script>
</body>

</html>